import axios from './http'
import { Session } from 'common-list'

// 获取所有题目
export async function getList(url, params = null, useCache = true) {
  const cacheKey = getFormatFullUrl(url, params)  // getCacheKeyByUrlAndParam(urlSet, params);

  let listData = null
  if (useCache) { //如果确定使用缓存
    listData = Session.get(cacheKey);
  }
  let hasData = false
  if (!listData) {//缓存中没有，则去服务器
    if (!params) {
      params = {}
    }
    const result = await axios.get(url, { params })
    //  await request.get<T>(urlSet.apiUrl,
    //     { params }
    // );
    // listData = { list: result.data, totalCount: result.totalCount }
    listData = result.data
    hasData = listData && listData.length > 0
    if (useCache && hasData) {//拥有数据的，才进行缓存,
      Session.set(cacheKey, listData);
    }
  } else {
    hasData = listData && listData.length > 0
  }

  if (hasData && params && params.searchText && params.searchFieldNames) {
    const searchFields = params.searchFieldNames
    const sText = params.searchText
    const filterData = listData.filter((dataItem) => {  //list.
      let r = false
      for (let i = 0; i < searchFields.length; i++) {
        const fname = searchFields[i]
        if (dataItem[fname] && dataItem[fname].toString().indexOf(sText) != -1) {
          r = true;
        }
      }
      return r;
    })

    listData = filterData // .list= { list: filterData, totalCount: dataFromCache.totalCount }
  }

  return listData

}


export function getFormatFullUrl(url, data) {
  let params = ``;
  for (let key in data) {
    if (data[key] != null && data[key] != '') {
      if (params.length == 0) {
        params += `?${key}=${data[key]}`;
      } else {
        params += `&${key}=${data[key]}`;
      }
    }
  }
  url += params;
  return url;
}
// 获取所有科目
export async function getSubjectList(data) {

  return await getList('/q_api/subject/list', data)
}
// 获取所有题目
export async function getQuestionList(data) {
  return await getList('/q_api/question/list', data)
}

//获取对应题目的数据
export async function getQuestion(qid) {
  return await getList(`/q_api/question/${qid}`)//这个不应该是getList啊、。。
}
// 获取题组内的所有题目，应该是根据刷题数获取！！这里不要用缓存！！！
export async function getQuestionGroupAllSub(gid, qidArray) {
  const params = { gid, qidArray }
  let useCache = false //默认不用缓存！！
  if (qidArray && qidArray.length) {
    useCache = true
  }
  return await getList('/q_api/questiongroup/getallquestions', params, useCache)
}


// 创建做题任务
export function createJob(data) {
  return axios({
    url: '/q_api/job/createjob',
    method: "post",
    data
  })
}

// 修改做题任务
export function updateJob(data) {
  return axios({
    url: '/q_api/job/updatejob',
    method: "post",
    data
  })
}

// 获取做题任务
export function getJob(data) {
  return axios({
    url: `/q_api/job/${data.session_id}`,
    method: 'get'
  })
}

// 获取做题任务
export function getJobList(params) {
  return axios({
    url: `/q_api/job/list`,
    method: 'get',
    params
  })
}

export function resetVoteItemList(data) {
  return axios({
    url: `/api/voteitem/resetlist`,
    method: 'post',
    data
  })
}
export function resetVoteItemListCache(voteID) {
  const url = `/api/voteitem/list`
  const cacheKey = getFormatFullUrl(url, { voteID })
  Session.remove(cacheKey)

}
export function resetJobListCache(gid) {
  const url = `/q_api/job/list`
  const url2 = '/q_api/job/rankinglist'
  const cacheKey = getFormatFullUrl(url, { gid })
  const cacheKey2 = getFormatFullUrl(url2, { gid })
  Session.remove(cacheKey)
  Session.remove(cacheKey2)
}
// 获取做题任务
export function resetJobList(data) {
  return axios({
    url: `/q_api/job/resetjoblist`,
    method: 'post',
    data
  })
}
// 获取做题任务
export function getRankingJobList(params) {
  return axios({
    url: `/q_api/job/rankinglist`,
    method: 'get',
    params
  })
}

export async function getIP() {
  return await axios({
    url: 'http://pv.sohu.com/cityjson?ie=utf-8', headers: {
      "Content-Type": "application/octet-stream",
      "Access-Control-Allow-Origin": "*",
    }
  })
}
// 更新学员学习数据,这里只是学生更新学习数据！！
export function getChooseUpdate(data) {
  return axios({
    url: `/api/choose/updatetaskresult`,
    method: 'post',
    data
  })
}

// 导入题目
export function importQuestion(data) {
  return axios({
    url: '/q_api/question/importquestions',
    method: 'post',
    data
  })
}

// 更新token
export function refreshToken(data) {
  return axios({
    url: '/api/user/refreshjwttoken',
    method: 'post',
    data
  })
}

export async function getClassList(data) {

  return await getList('/api/teacher/classlist', data)
}